
package com.dailytechlab.controller;

import com.dailytechlab.model.Module;
import com.dailytechlab.repository.ModuleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/modules")
@CrossOrigin(origins = "http://localhost:3000")
public class ModuleController {
    
    @Autowired
    private ModuleRepository moduleRepository;
    
    @GetMapping
    public List<Module> getAllModules() {
        return moduleRepository.findAll();
    }
    
    @GetMapping("/{id}")
    public ResponseEntity<Module> getModuleById(@PathVariable Long id) {
        return moduleRepository.findById(id)
                .map(module -> ResponseEntity.ok().body(module))
                .orElse(ResponseEntity.notFound().build());
    }
    
    @PostMapping
    public Module createModule(@RequestBody Module module) {
        return moduleRepository.save(module);
    }
}
